package com.njtoyo.taxi.admin.rest.controller.platform;

import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.wrapper.platform.role.RoleCreateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.platform.role.RoleListWrapper;
import com.njtoyo.taxi.admin.service.platform.RoleService;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/platform/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("")
    @ApiOperation(value = "获取角色列表", notes = "权限: role")
    @PermissionScope(names = {"role"})
    public RestResult getList(@Valid RoleListWrapper roleListWrapper) {
        return roleService.findRolePage(roleListWrapper);
    }

    @GetMapping("/{roleId}/resource")
    @ApiOperation(value = "获取角色权限", notes = "权限: role")
    @PermissionScope(names = {"role"})
    public RestResult getResource(@PathVariable("roleId")Long roleId) {
        return roleService.findResource(roleId);
    }

    @PostMapping("")
    @ApiOperation(value = "创建角色", notes = "权限: role_create")
    @PermissionScope(names = {"role_create"})
    public RestResult create(@Valid @RequestBody RoleCreateWrapper roleCreateWrapper) {
        return roleService.create(roleCreateWrapper);
    }

    @PatchMapping("/{roleId}")
    @ApiOperation(value = "修改角色", notes = "权限: role_update")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "body", dataType = "json", name = "params", value = "title: \"string\" 角色名称,可选; resourceIds: [] 资源ID集合,可选") })
    @PermissionScope(names = {"role_update"})
    public RestResult update(@PathVariable("roleId")Long roleId, @RequestBody Map params) {
        return  roleService.update(roleId, params);
    }

    @DeleteMapping("/{roleId}")
    @ApiOperation(value = "删除角色", notes = "权限: role_delete")
    @PermissionScope(names = {"role_delete"})
    public RestResult delete(@PathVariable("roleId")Long roleId) {
        return roleService.delete(roleId);
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取全部角色", notes = "无权限校验")
    public RestResult getAll() {
        return roleService.getAll();
    }
}
